package com.flowerstore.service;

import com.flowerstore.entity.Flower;
import com.flowerstore.repository.FlowerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowerService {
    
    private static final Logger logger = LoggerFactory.getLogger(FlowerService.class);
    
    @Autowired
    private FlowerRepository flowerRepository;
    
    // 查询所有鲜花
    public List<Flower> getAllFlowers() {
        logger.info("获取所有鲜花信息");
        return flowerRepository.findAll();
    }
    
    // 添加新鲜花
    public Flower addFlower(Flower flower) {
        logger.info("添加新鲜花: {}", flower.getName());
        return flowerRepository.save(flower);
    }
    
    // 更新鲜花信息
    public Flower updateFlower(Long id, Flower flowerDetails) {
        logger.info("更新鲜花信息，ID: {}", id);
        Flower flower = flowerRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("未找到指定ID的鲜花: " + id));
        
        flower.setName(flowerDetails.getName());
        flower.setPrice(flowerDetails.getPrice());
        flower.setDescription(flowerDetails.getDescription());
        flower.setIsInStock(flowerDetails.getIsInStock());
        
        return flowerRepository.save(flower);
    }
    
    // 删除鲜花
    public void deleteFlower(Long id) {
        logger.info("删除鲜花，ID: {}", id);
        flowerRepository.deleteById(id);
    }
} 